package com.onesports.intelligent.k12.polarlight.controller.sys;


import cn.oneframework.common.utils.ResultUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.onesports.intelligent.k12.polarlight.constant.ApiPathConstants;
import com.onesports.intelligent.k12.polarlight.domain.dto.common.IdDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.sys.InsertBannerDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.sys.UpdateBannerDTO;
import com.onesports.intelligent.k12.polarlight.domain.entity.sys.BannerConfig;
import com.onesports.intelligent.k12.polarlight.security.SecurityUtils;
import com.onesports.intelligent.k12.polarlight.service.sys.BannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author wangcx
 * @since 2022-7-8
 */
@Api(value = "运营端-小程序设置", tags = "运营端-小程序设置")
@RestController
@RequestMapping(ApiPathConstants.OPERATION_BANNER)
@RequiredArgsConstructor
@PreAuthorize("isAuthenticated()")
public class BannerController {

    @Resource
    private final BannerService service;

    @PostMapping("/add")
    @ApiOperation(value = "添加轮播图")
    public ResponseEntity add(@Valid @RequestBody InsertBannerDTO dto)
    {
        service.add(dto);
        return ResultUtil.success("添加成功");
    }


    @PostMapping("/upd")
    @ApiOperation(value = "修改轮播图")
    public ResponseEntity upd(@Valid @RequestBody UpdateBannerDTO dto)
    {
        service.upd(dto);
        return ResultUtil.success("修改成功");
    }


    @PostMapping("/del")
    @ApiOperation(value = "删除轮播图")
    public ResponseEntity del(@RequestBody IdDTO dto)
    {
        service.del(dto);
        return ResultUtil.success("删除成功");
    }

    @GetMapping("/page")
    @ApiOperation(value = "查询轮播图列表", response = BannerConfig.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "row", value = "条数", required = true, dataType = "long", paramType = "query"),
    })
    public ResponseEntity<IPage<BannerConfig>> findPage(@RequestParam(value = "page") Long page,
                                                        @RequestParam(value = "row") Long row) {
        String organizationId = SecurityUtils.getOrganizationId();
        return ResultUtil.success(this.service.findPage(new Page<>(page, row), organizationId));

    }

}